﻿/*
 * Configura Ext.Direct
 */
Ext.Direct.addProvider(Ext.app.REMOTING_API);
Ext.Direct.on('exception',function(e)
{	
	var	details = [];
	
	if(e.xhr.statusText)
	{ 
		details.push(e.xhr.statusText);
	}
	
	if(e.transaction.action && e.transaction.method)
	{ 
		details.push(e.transaction.action + '.' + e.transaction.method); 
	}
	
	Ext.Msg.show.defer(10,Ext.Msg,[{
		 title	: 'Ops! Ocorreu um erro no servidor...'
		,msg	: 'Tente novamente ou contate o administrador.<br /><span style="font-size:10px; color:#666; text-align:right;">Detalhes: ' + details.join(', ') + '</span>'
		,buttons: Ext.Msg.OK
		,icon	: Ext.MessageBox.ERROR
	}]);
});

/*
 * Cria layout
 */
Ext.onReady(function() 
{
	/*
	 * Permanencia de estados
	 */
   Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
		expires	: new Date().add(Date.DAY,30)
   }));
   
	var onMenuItemClick = function(link)
	{
		var tabPanel = Ext.getCmp('main-tabpanel');
		var target = link.target;
		
		if(!target){return;}
		
		Bt.require(target,function()
		{
			var tab = tabPanel.items.find(function(item){ return item.title === link.title; });
			
			if(!tab)
			{
				
				var _class = window[link.target]||window[link.target+'Lista']||window[link.target+'Cadastro'];
				var _instance = new _class({
					 title		: link.title||link._title
					,iconCls	: link.iconCls
					,closable	: true
				});
				
				tab = tabPanel.add(_instance);
			}
			
			tabPanel.activate(tab);
		});
	};
	
	var hideIcons = function()
	{
		var panel = Ext.getCmp('painel-lateral');
		panel.items.each(function(item)
		{
			item._title = item.title;
			item.setTitle('');
		});
		
		panel._width = panel.getWidth();
		panel.setWidth(30);
		panel.setTitle('');
		
		if(panel.collapsed)
		{
			panel.expand();
		}
		else
		{
			panel.ownerCt.doLayout();
		}
		
		panel.tools.minus.hide();
		panel.tools.plus.show();
	};
	
	var showIcons = function()
	{
		var panel = Ext.getCmp('painel-lateral');
		panel.items.each(function(item)
		{
			item.setTitle(item.title||item._title);
		});
		
		panel.setWidth(panel._width||160);
		panel.setTitle('Menu');
		
		panel.tools.minus.show();
		panel.tools.plus.hide();
		
		panel.ownerCt.doLayout();
	};
	
	var getState = function(panel)
	{
		return {
			 title		: this.title
			,_title		: this._title
			,width		: this.el.getWidth()
			,_width		: this._width
			,plus		: this.tools.plus.isVisible()
		};
	};
	
	var applyState = function(state)
	{
		if(state)
		{
	        Ext.apply(this, state);
			
			if(state.plus)
			{
				this.items.each(function(item)
				{
					item._title = item.title;
					item.setTitle('');
				});
				
				this.on('render',function()
				{
					this.tools.minus.hide();
					this.tools.plus.show();
		
				},this,{single:true});
			}
	    }
	};
	
    new Ext.Viewport({
		 layout	: 'border'
		,border	: false
       	,items	: [{
			 xtype	: 'toolbar'
			,region	: 'north'
			,margins: '0 0 5 0'
			,cls	: 'main-toolbar'
			,height	: 30
			,items	: ['<h1 class="titulo-tst">Tst - Gerenciamento de Testes<h1>',{
				 xtype: 'tbspacer'
				,width: 20
			},'-',{
				 text	: 'Logoff'
				,iconCls: 'ico_door_in'
				,scope	: this
				,handler: function()
				{
					Ext.Msg.confirm('Confirmação','Deseja realmente sair do sistema?',function(opt)
					{ 
						if(opt==='yes')
						{
							window.location.href = '/Home/Login';
						}
					});
				}
			},{
				 text	: 'Meu cadastro'
				,iconCls: 'ico_user_edit'
				,scope	: this
				,handler: function()
				{
					Ext.Msg.alert('Ops!','Interface ainda em desenvolvimento...');
				}
			}]
		},{
			  xtype			: 'panel'
			 ,layout		: 'links'
			 ,region		: 'west'
			 ,title			: 'Menu'
			 ,cmargins		: '0 5 0 0'
			 ,id			: 'painel-lateral'
			 ,stateId		: 'painel-lateral'
			 ,stateEvents	: ['resize']
			 ,getState		: getState
			 ,applyState	: applyState
			 ,stateful		: true
			 ,split			: true
			 ,width			: 160
			 ,minWidth		: 130
			 ,maxWidth		: 300
			 ,defaults		: {
			 	 border		: false
				,scope		: this
				,handler	: onMenuItemClick
				,defaults	: {
					 scope	: this
					,handler: onMenuItemClick
				}				
			}
			,tools: [{
				 id		: 'minus'
				,handler: hideIcons
			},{
				 id		: 'plus'
				,hidden	: true
				,handler: showIcons
			}]
		 	,items			: [{
				 title	: 'Portal'
				,iconCls: 'ico_house'
				,target	: 'Portal' 
			},{
				 title	: 'Usuarios'
				,iconCls: 'ico_user'
				,target	: 'Usuario' 
		 	},{
			 	 title	: 'Projetos'
				,iconCls: 'ico_lightning'
				,target	: 'Projeto' 
		 	},{
			 	 title	: 'M&#243;dulos'
				,iconCls: 'ico_plugin'
				,target	: 'Modulo'
			},{
			 	 title	: 'Ambientes de Teste'
				,iconCls: 'ico_images'
				,target	: 'AmbienteTeste'
			},{
				 title	: 'Tipos de erro'
				,iconCls: 'ico_error'
				,target	: 'TipoErro'
			},{
			 	title	: 'Planos de Testes'
				,iconCls: 'ico_blueprint'
				,target	: 'PlanoTeste'
			},{
			 	 title	: 'Casos de Testes'
				,iconCls: 'ico_list_numbers'
				,target	: 'CasoTeste'
			},{
				 title	: 'Planos de Execução'
				,iconCls: 'ico_blueprint_play'
				,target	: 'PlanoExecucao'
			}]
		},{
             xtype  	: 'tabpanel'
            ,region 	: 'center'
			,id			: 'main-tabpanel'
			,plain		: true
			,activeItem	: 0
			,items		: [new Portal({
				 title	: 'Portal'
				,iconCls:'ico_house'
			})]
        }]
    });
	
});